package org.anachronos.jeep.ui.attach;

import org.anachronos.jeep.core.agent.ProfilerRemoteControl;
import org.anachronos.jeep.core.attach.VirtualMachineAttachement;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Abstract handler class that provides access to the selected {@link VirtualMachineAttachement}.
 * 
 * @author km
 */
public abstract class VirtualMachineAttachementCommand extends AbstractHandler {
	public VirtualMachineAttachement getSelection(final ExecutionEvent event) 
			throws ExecutionException {
		final IStructuredSelection currentSelection = 
			(IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
		final VirtualMachineAttachement vmToAttach = 
			(VirtualMachineAttachement) currentSelection.iterator().next();
		return vmToAttach;
	}
	
	public ProfilerRemoteControl getProfilerRemoteControl(final ExecutionEvent event) 
			throws ExecutionException {
		final VirtualMachineAttachement selection = getSelection(event);
		ProfilerRemoteControl profilerRemoteControl;
		try {
			profilerRemoteControl = selection.getProfilerRemoteControl();
		} catch (final Exception e) {
			throw new ExecutionException("", e);
		}
		return profilerRemoteControl;
	}
}
